package com.study.mike.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.study.mike.common.annotation.MyLog;
import com.study.mike.common.constants.Constant;
import com.study.mike.common.enums.MethodTypeEnum;
import com.study.mike.model.po.DeleteMagicalPO;
import com.study.mike.model.po.MagicalLandPO;
import com.study.mike.model.po.MagicalQueryPO;
import com.study.mike.result.R;
import com.study.mike.result.RUtil;
import com.study.mike.service.MagicalLandService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author KangHui
 * @Date 2022/12/23 16:28
 */
@RestController
@RequestMapping("/magical")
public class MagicalLandController {
    @Resource
    private MagicalLandService magicalLandService;
    // 1、缓存

    /**
     * 新增数据
     *
     * @param po
     * @return
     */
    @PostMapping("/add")
    @MyLog(title = "magicalAdd", methodType = MethodTypeEnum.INSERT)
    public R addMagical(@RequestBody MagicalLandPO po) {
        if (StrUtil.isBlank(po.getGameNo())) {
            return RUtil.error("编号不能为空");
        }
        return RUtil.success(magicalLandService.addMagical(po));
    }

    /**
     * 编辑数据
     *
     * @param po
     * @return
     */
    @PostMapping("/update")
    @MyLog(title = "magicalUpdate", methodType = MethodTypeEnum.UPDATE)
    public R updateMagical(@RequestBody MagicalLandPO po) {
        if (ObjectUtil.isNull(po.getId())) {
            return RUtil.error("操作数不能为空");
        }
        return RUtil.success(magicalLandService.updateMagical(po));
    }

    /**
     * 分页查询
     *
     * @param po
     * @return
     */
    @PostMapping("/query")
    @MyLog(title = "magicalQuery", methodType = MethodTypeEnum.SELECT)
    public R queryMagical(@RequestBody MagicalQueryPO po) {
        return RUtil.success(magicalLandService.queryMagical(po));
    }

    /**
     * 批量移除
     *
     * @param po
     * @return
     */
    @PostMapping("/delete/batch")
    @MyLog(title = "magicalDelete", methodType = MethodTypeEnum.DELETE)
    public R deleteMagicalBatch(@RequestBody DeleteMagicalPO po) {
        if (CollUtil.isEmpty(po.getMagicalLandPOS())) {
            return RUtil.error("操作数据不能为空");
        }
        return RUtil.success(magicalLandService.deleteMagicalBatch(po));
    }

    /**
     * magicLand数据导出
     */
    @PostMapping("/export")
    public R exportMagicLand(@RequestBody MagicalQueryPO po) {
        if (ObjectUtil.isEmpty(po.getExportType())) {
            return RUtil.fail("请选择导出方式");
        }
        if (ObjectUtil.equal(po.getExportType(), Constant.ExportType.SELECT) && CollUtil.isEmpty(po.getIds())) {
            return RUtil.fail("导出所选时，所选数据不能为空");
        }
        return RUtil.success(magicalLandService.exportMagicLand(po));
    }

    /**
     * magicLand数据导人
     *
     * @return
     */
    @PostMapping("/import")
    public R importMagicLand() {
        return RUtil.success(magicalLandService.importMagicLand());
    }


}
